//
//  CommClass.m
//  KingProFrame
//
//  Created by JinLiang on 15/8/5.
//  Copyright (c) 2015年 king. All rights reserved.
//

#import "CommClass.h"
#import <sys/sysctl.h>
#import <mach/mach.h>
#import <Security/Security.h>
#import "SSKeychain.h"



static NSMutableDictionary  *commonDict;

@implementation CommClass

@synthesize data = _data;

//初始化
- (id)init
{
    self = [super init];
    if (self) {
        self.data = [NSMutableDictionary dictionary];
    }
    return self;
}


//单例对外方法
+ (CommClass *)sharedCommon
{
    static CommClass *sharedCommon;
    
    @synchronized(self) {
        if (!sharedCommon) {
            sharedCommon = [[self alloc] init];
        }
        return sharedCommon;
    }
}

#pragma -mark 保存在内存中的方法
//非空赋值
- (void)setObject:(id)aObject forKey:(id)aKey
{
    if (aObject != nil && aKey != nil) {
        [_data setObject:aObject forKey:aKey];
    }
}
//取值
- (id)objectForKey:(id)aKey
{
    return [_data objectForKey:aKey];
}

-(void)removeObjectForKey:(id)aKey
{
    NSArray * keys = [_data allKeys];
    if (keys != nil && keys.count != 0 && [keys containsObject:aKey]) {
        [_data removeObjectForKey:aKey];
    }
}

#pragma -mark 保存在本地

//非空赋值
- (void)localObject:(id)aObject forKey:(id)aKey
{
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    if (aObject != nil && aKey != nil) {
        [defaults setObject:aObject forKey:aKey];
    }
    [defaults synchronize];
}
//取值
- (id)localObjectForKey:(id)aKey
{
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    return [defaults objectForKey:aKey];
}
- (void)removeLocalObjectForKey:(id)akey{

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if ([defaults objectForKey:akey] != nil) {
        [defaults removeObjectForKey:akey];
        [defaults synchronize];
    }
}
/**是否第一次运行*/
-(BOOL)isFirstLoad
{
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    BOOL  hasRunBefore = [defaults boolForKey:@"isFirstLoad"];
    if(!hasRunBefore)
    {
        //DLog(@"这是第一次运行");
        //朱矿外网
        [defaults setObject:@"182.141.26.181" forKey:IP];
        [defaults setObject:@"8082" forKey:PORT];
        //朱矿内网
//        [defaults setObject:@"192.168.2.213" forKey:IP];
//        [defaults setObject:@"8001" forKey:PORT];
        [defaults setBool:YES forKey:@"isFirstLoad"];
        [defaults synchronize];
        return YES;
    }
    return NO;
}

//时间转换成时间字符串 timeFormat是要转换成的时间格式
+(NSString*)formatTimeStamp:(NSDate *)timeDate timeFormat:(NSString *)timeFormat{
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //设定时间格式,这里可以设置成自己需要的格式
    [dateFormatter setDateFormat:timeFormat];
//    [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:8]];
    return [dateFormatter stringFromDate: timeDate];
}

//时间字符串转换成时间 timeFormat是要转换成的时间格式
+(NSDate*)dateFormatTimeStamp:(NSString *)timeString timeFormat:(NSString *)timeFormat
{
    NSDateFormatter *formatter=[[NSDateFormatter alloc]init];
    [formatter setDateFormat:timeFormat];
//    [formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:8]];
    return [formatter dateFromString:timeString];
}

+ (void)postAlertViewControllerWithTitle:(NSString *)title message:(NSString *)message cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitle:(NSString *)otherButtonTitle cancelEvent:(void (^)())cancelBlock otherEvent:(void (^)())otherBlock viewController:(id)viewController{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *cancelAction=nil;
    UIAlertAction *otherAction=nil;
    if (cancelButtonTitle) {
        cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action)
                        {
                            //cancel Button
                            if (cancelBlock) {
                                cancelBlock();
                            }
                        }];
        [alertController addAction:cancelAction];
    }
    if (otherButtonTitle) {
        otherAction = [UIAlertAction actionWithTitle:otherButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action)
                       {
                           if (otherBlock) {
                               otherBlock();
                           }
                       }];
        [alertController addAction:otherAction];
    }
    
    [viewController presentViewController:alertController animated:YES completion:nil];
}
//获取IP
+ (NSString *)getIPStr{
    return [[NSUserDefaults standardUserDefaults]objectForKey:IP];
}
//获取端口号
+ (NSString *)getPortStr{
    return [[NSUserDefaults standardUserDefaults]objectForKey:PORT];
}

+ (NSString *)getServerURL{
    return [NSString stringWithFormat:@"http://%@:%@/ZKYHService.asmx",[[NSUserDefaults standardUserDefaults]objectForKey:IP],[[NSUserDefaults standardUserDefaults]objectForKey:PORT]];
}

+ (NSString *)getUuidWithKeyChain {
    NSString *strUUID = [SSKeychain passwordForService:@"com.LNjujin.GEARPAD"account:@"user"];
    if (strUUID == nil || [strUUID isEqualToString:@""])
    {
        CFUUIDRef uuid = CFUUIDCreate( nil );
        CFStringRef uuidString = CFUUIDCreateString( nil, uuid );
        NSString * result = (NSString *)CFBridgingRelease(CFStringCreateCopy( NULL, uuidString));
        CFRelease(uuid);
        CFRelease(uuidString);
        [SSKeychain setPassword:strUUID
                     forService:@"com.LNjujin.GEARPAD"account:@"user"];
        return result;
    }else{
        return strUUID;
    }
}

+ (NSString *)fullTimeToCertainTime:(NSString *)fullTime{
    
    if (fullTime == nil ) {
        return @"00:00";
    }
    if (fullTime.length != 19) {
        return [fullTime substringWithRange:NSMakeRange(fullTime.length-5, 5)];
    }
    NSString *certainTime = [fullTime substringWithRange:NSMakeRange(11, 5)];
    return certainTime;
    
}


+ (NSString *)getMMSSFromSS:(NSString *)totalTime{
    
    NSString *format_time = nil;
    if ([totalTime floatValue] == 0) {
        format_time = @"0小时";
    }else{
        format_time = [NSString stringWithFormat:@"%.1f小时",[totalTime floatValue]/60];
    }

    return format_time;
}

/**
 * 拼接时间标题 向下的箭头拼接在文本后面 形成富文本
 * titlestr 标题文本
 * imgName 图片名称
 * y 图片的坐标
 */
+(NSAttributedString *)getTitleStr:(NSString *)titlestr withImgName:(NSString *)imgName withImgY:(float)y
{
    // NSTextAttachment - 附件
    NSTextAttachment *attachMent = [[NSTextAttachment alloc] init];
    // 为附件设置图片
    attachMent.image = [UIImage imageNamed:imgName];
    attachMent.bounds=CGRectMake(0, -y, attachMent.image.size.width, attachMent.image.size.height);
    // 键附件添加到图文混排
    NSMutableAttributedString * nameStr = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@  ",titlestr]];
    NSAttributedString *str = [NSAttributedString attributedStringWithAttachment:attachMent];
    [nameStr appendAttributedString:str];

    return nameStr;
    
}

+ (RolePower)getRolePowerWith:(NSString *)powerStr {
    
    if ([powerStr isEqualToString:@"1"]) {
        return RolePowerAdmin;
    }else if ([powerStr isEqualToString:@"2"]){
        return RolePowerYZLeader;
    }else if ([powerStr isEqualToString:@"3"]){
        return RolePowerCLLeader;
    }else{
        return RolePowerInternalLeader;
    }
}


/**
 上拉加载方法

 @param tableView    列表
 @param refreshblock 刷新的方法

 @return MJRefreshFooter
 */
+ (MJRefreshFooter *)setUpFooterRefresh:(id)tableView withBlock:(MJRefreshComponentRefreshingBlock)refreshblock
{
    UITableView *refreshTableview = (UITableView *)tableView;
    
    MJRefreshBackNormalFooter *footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:refreshblock];
    
    refreshTableview.mj_footer = footer;
    [footer setTitle:@"正在为您努力加载！" forState:MJRefreshStateRefreshing];
    
    refreshTableview.mj_footer.automaticallyChangeAlpha = YES;
    
   return refreshTableview.mj_footer;
}

+ (NSString *)getTodayDateStr{
    
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
    [dateFormat setDateFormat:NEW_TIME_FORMATTER_DATE];
    NSString *dateStr = [dateFormat stringFromDate:[NSDate date]];
    return dateStr;
}

/**
 * 功能：去除字符串左右两边的字符
 * message  被剪切的字符串
 * subString 要剪切的字符
 */
+(NSString*)subBeforAndAfterChar:(NSString *)message WithSubChar:(NSString *)subString
{
    NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:subString];
    message = [message  stringByTrimmingCharactersInSet:set];
    return message;
}

@end
